<?php
namespace app\controller\api;

use app\exception\ErrnoPlus;
use app\service\VersionInfoService;
use app\validate\VersionInfoValidate;
use share\controller\ApiBaseController;
use share\service\ContextService;
use think\Log;

class VersionInfoController extends ApiBaseController{
    
    public function initValidate(){  
        $this->validate = new VersionInfoValidate();
    }


    public function get_action(){
        $input = $this->check();
        $packageName = $this->request->header("pack-name");
        if(!empty($packageName)){
            ContextService::instance()->set('packageName',$packageName);
        }
        $input['type'] = strtolower($input['type']);
        $versionInfo = VersionInfoService::getLastOne($input,$packageName);
        if(empty($versionInfo)){ // 保持与之前版本统一
            return self::fail("no data",ErrnoPlus::NOT_BACK_DATA);
        }
        return self::ok($versionInfo);
    }

    public function add_action(){
        $input = $this->check();
        return self::ok(VersionInfoService::appUpdate($input));
    }

    public function all_action(){
        $input = $this->check();
        $clientType = ContextService::getClientType();
        return self::ok(VersionInfoService::getSummaryInfo($input,true,$clientType));
    }

    public function all_v2_action(){
        $input = $this->check("all");
        $clientType = ContextService::getClientType();
        return self::ok(VersionInfoService::getSummaryInfo($input,false,$clientType));
    }

  
}